using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Design;

namespace LibreriaWC
{
    /// <summary>
    /// WebControl que muestra una solapa.
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:btnSolapa runat=server></{0}:btnSolapa>")]
    public class btnSolapa : WebControl
    {
        public event EventHandler SolapaClick;
        Image img1 = new Image();
        string imagenFondo;
        Image img2 = new Image();

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        [DefaultValue("")]
        [Bindable(true)]
        [Editor("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        [UrlProperty]
        public string ImagenIzquierdaOn
        {
            get
            {
                String s = (String)ViewState["ImagenIzquierdaOn"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["ImagenIzquierdaOn"] = value;
            }
        }

        [DefaultValue("")]
        [Bindable(true)]
        [Editor("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        [UrlProperty]
        public string ImagenDerechaOn
        {
            get
            {
                String s = (String)ViewState["ImagenDerechaOn"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["ImagenDerechaOn"] = value;
            }
        }

        [DefaultValue("")]
        [Bindable(true)]
        public string ImagenFondoCentroOn
        {
            get
            {
                String s = (String)ViewState["ImagenFondoCentroOn"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["ImagenFondoCentroOn"] = value;
            }
        }

        [DefaultValue("")]
        [Bindable(true)]
        [Editor("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        [UrlProperty]
        public string ImagenIzquierdaOff
        {
            get
            {
                String s = (String)ViewState["ImagenIzquierdaOff"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["ImagenIzquierdaOff"] = value;
            }
        }

        [DefaultValue("")]
        [Bindable(true)]
        [Editor("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        [UrlProperty]
        public string ImagenDerechaOff
        {
            get
            {
                String s = (String)ViewState["ImagenDerechaOff"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["ImagenDerechaOff"] = value;
            }
        }

        [DefaultValue("")]
        [Bindable(true)]
        public string ImagenFondoCentroOff
        {
            get
            {
                String s = (String)ViewState["ImagenFondoCentroOff"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["ImagenFondoCentroOff"] = value;
            }
        }

        public override void ApplyStyleSheetSkin(Page page)
        {
            base.ApplyStyleSheetSkin(page);
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Seleccionada
        {
            get
            {
                return (ViewState["Seleccionada"] == null ? false : (Boolean)ViewState["Seleccionada"]);
            }

            set
            {
                ViewState["Seleccionada"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            base.RenderContents(output);
        }

        protected override void CreateChildControls()
        {
            Table tabla = new Table();
            tabla.CellPadding = 0;
            tabla.CellSpacing = 0;
            tabla.Height = this.Height;
            TableRow fila = new TableRow();
            tabla.Rows.Add(fila);

            img1.ImageUrl = ImagenIzquierdaOff;
            imagenFondo = ImagenFondoCentroOff;
            img2.ImageUrl = ImagenDerechaOff;

            TableCell celda = new TableCell();

            celda.Controls.Add(img1);
            fila.Cells.Add(celda);
            celda = new TableCell();
            celda.Wrap = false;
            celda.Style.Add(HtmlTextWriterStyle.BackgroundImage, HttpContext.Current.Request.ApplicationPath + imagenFondo);
            LinkButton lb = new LinkButton();

            if (this.SkinID != null && this.SkinID != "")
                lb.SkinID = this.SkinID;

            if (this.CssClass != null && this.CssClass != "")
                lb.CssClass = this.CssClass;

            lb.ApplyStyleSheetSkin(base.Page);
            lb.Text = Text;
            lb.Font.CopyFrom(this.Font);
            lb.ForeColor = this.ForeColor;
            lb.Click += new EventHandler(lb_Click);
            lb.CausesValidation = false;
            celda.Controls.Add(lb);
            fila.Cells.Add(celda);

            celda = new TableCell();
            celda.Controls.Add(img2);
            fila.Cells.Add(celda);

            this.Controls.Add(tabla);

            base.CreateChildControls();
        }

        protected void lb_Click(object sender, EventArgs e)
        {
            Seleccionada = true;
            img1.ImageUrl = ImagenIzquierdaOn;
            imagenFondo = ImagenFondoCentroOn;
            img2.ImageUrl = ImagenDerechaOn;

            if (SolapaClick != null)
                SolapaClick(this, e);
        }
    }
}
